﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL.BussinessClass;
using Model;
namespace AlltrustDinner.Pages.Users
{
    /// <summary>
    /// UserProcess 的摘要说明
    /// </summary>
    public class UserProcess : HttpHandlerProcess, IHttpHandler
    {
        private string result = string.Empty;
        public void ProcessRequest(HttpContext context)
        {
            base.GetQueryParam(context);

            switch (cmd)
            {
                case "OrderNewItem":
                    result = this.OrderNewItem(queryParam["order_info"]);
                    break;
                case "DeleteItem":
                    result = this.DeleteItem(queryParam["item_id"]);
                    result = this.Serializer.Serialize(result);
                    break;
                default:
                    break;
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(result);
        }

        private string DeleteItem(Dictionary<string, string> param)
        {
            OrderBiz biz = new OrderBiz();
            return biz.DeleteItem(DateTime.Now.ToString("yyyyMMdd"), param["hid_id"]).ToString();

        }

        private string OrderNewItem(Dictionary<string, string> param)
        {
            Dictionary<string, Object> returnResult = new Dictionary<string, Object>();
            OrderBiz biz = new OrderBiz();
            UserBiz userBiz = new UserBiz();
            ITEM item = new ITEM();
            USER currUser = HttpContext.Current.Session["CurrentUser"] as USER;
            if (null == currUser)
            {
                returnResult.Add("Result", "False");
                return Serializer.Serialize(returnResult);
            }
            decimal price = 0;
            if (!decimal.TryParse(param["hid_price"], out price))
            {
                returnResult.Add("Result", "False");
                return Serializer.Serialize(returnResult);
            }
            item.NAME = currUser.NAME;
            item.RESTURANT = param["resturant_name"];
            item.DINNER = param["menu_name"];
            item.PRICE = price;
            item.TIME = DateTime.Now;
            item.REMARK = param["ipt_remark"];
            biz.OrderItem(item);
            currUser.BALANCE -= price;
            userBiz.UpdateUser(currUser);
            List<ITEM> itemList = biz.LoadTodayOrderItemList();


            returnResult.Add("ItemList", itemList);
            returnResult.Add("ItemCount", itemList.Count(c => c.NAME == currUser.NAME));
            returnResult.Add("Result", "TRUE");
            return Serializer.Serialize(returnResult);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}